WebsiteAgent: Improve readability with regard to XPath evaluation.

Akinori MUSHA 10 years ago
parent
commit
76ecced268
1 changed files with 14 additions and 8 deletions
  1. 14 8
      app/models/agents/website_agent.rb

+ 14 - 8
app/models/agents/website_agent.rb

@@ -154,17 +154,23 @@ module Agents
154 154
                   error '"css" or "xpath" is required for HTML or XML extraction'
155 155
                   return
156 156
                 end
157
-                unless Nokogiri::XML::NodeSet === nodes
157
+                case nodes
158
+                when Nokogiri::XML::NodeSet
159
+                  result = nodes.map { |node|
160
+                    case value = node.xpath(extraction_details['value'])
161
+                    when Float
162
+                      # Node#xpath() returns any numeric value as float;
163
+                      # convert it to integer as appropriate.
164
+                      value = value.to_i if value.to_i == value
165
+                    when Nokogiri::XML::NodeSet
166
+                      value = value.first
167
+                    end
168
+                    value.to_s
169
+                  }
170
+                else
158 171
                   error "The result of HTML/XML extraction was not a NodeSet"
159 172
                   return
160 173
                 end
161
-                result = nodes.map { |node|
162
-                  value, = node.xpath(extraction_details['value'])
163
-                  if value.is_a?(Float) && value.to_i == value
164
-                    value = value.to_i
165
-                  end
166
-                  value.to_s
167
-                }
168 174
                 log "Extracting #{extraction_type} at #{xpath || css}: #{result}"
169 175
               end
170 176
               output[name] = result